Uec3.addOutPurchaseWindowCreator = Ext.extend(PurchaseWindowCreator, {
    id: 'addOutPurchaseCreator',
    
    
    getPurchaseItemGrid : function() {
		
		var purchase_item_colName = this.creator.getPurchaseItemColName.bind(this)();
				
		// 创建grid，装载明细数据
		return new Ext.grid.EditorGridPanel( {
			height : 280,
			clicksToEdit : 1,
			colModel : purchase_item_colName,
			tbar : this.toolbar,
			store : this.purchase_item_store,
			selModel : new Ext.grid.RowSelectionModel(
	                     {
	                        moveEditorOnEnter : true,
	                        singleSelect : true
	                     }
	        )
		});

	},
	getPanel : function(main_win, initParams) {
		
		this.setAllHidden.bind(main_win)();
		
		this.setAllCmb.bind(main_win)();
		
		main_win.purchase_item_record = this.getPurchaseItemRecord.bind(main_win)();
		
		main_win.purchase_item_store = this.getPurchaseItemStore.bind(main_win)();
		
		main_win.partner_combox = this.getPartnerCombox.bind(main_win)();

		main_win.quotation_item_store = this.getQuotationItemStore.bind(main_win)();

		main_win.quotation_item_grid = this.getQuotationItemGrid.bind(main_win)();
		
		main_win.quotation_form_item1 = this.getQuotationFormItem1.bind(main_win)();
		
		//创建工具栏组件
		main_win.toolbar = getToolBar();
		
		main_win.purchase_item_grid = this.getPurchaseItemGrid.bind(main_win)();
		
		main_win.address_combox = this.getAddressCombox.bind(main_win)();
		
		main_win.purchase_panel = this.getPurchasePanel.bind(main_win)();
		
		main_win.purchase_panel.setHeight(440);
		
		main_win.quotation_panel = this.getQuotationPanel.bind(main_win)();
		
		
		this.addEventToPurItemGrid.bind(main_win)();
		
		
		// 总布局面板
		main_win.join_panel = new Ext.Panel({
			frame : true,
			height : 470,
			autoScroll : true,
			items : [main_win.purchase_panel]
		});

		main_win.join_panel.main_win = main_win;
		
		/**
         * 根据零件code、source查找零件信息
         */	  
	    function autoFillDetailForm(){
	    	 var source = this.getValue();
	    	 var code = pur_detailForm.getForm().findField("code").getValue();
	    	 if(code){
	    	     Ext.Ajax.request({
	    	        url :Uec3.App.Constants.homeUrl+"/part/queryPart!getPartByCodeAndSource.do",
	                params :{queryParam:Ext.encode({code:code,source:source})},
	                method :"post",
	                callback :function(options,success,response){
	                     
	                },
	                success:function(response,options){	                	 
	                	  var resp = Ext.decode(response.responseText);
	                	  if(resp){
	                	     pur_detailForm.getForm().setValues(resp);
	                	     pur_detailForm.getForm().findField("partId").setValue(resp.id);
	                	     pur_detailForm.getForm().findField("orderQuantity").focus();
	                	     
	                	  }else{
	                	     Ext.MessageBox.alert("提示","此零件编码和来源无相关零件信息,请重试");
	                	     pur_detailForm.getForm().findField("source").el.dom.value='';
	                	     pur_detailForm.getForm().findField("name").el.dom.value='';
	                	     pur_detailForm.getForm().findField("measurementUnit").el.dom.value='';
	                	  }					       
	                },
	                failure:function(response,options){
	                      Ext.MessageBox.alert("提示","数据加载异常");
	                }
	    	   });	    		    	 
	       }else{
	         Ext.MessageBox.alert("提示","请填写零件编码");
	       }	    		         
	    }
		
		
		var ds_code_select = new Ext.data.JsonStore({
			url : PurchaseUrl.PUR_FIND_PART_BY_CODE_URL,
			root:'root',
			totalProperty:'totalProperty',
		    fields:[{name:'code',type:'string'}]
		});
		
		
		
		var pur_detailForm = new Ext.form.FormPanel({
	        labelSeparator : "：",
			frame:true,
			border:false,
			defaults:{width:246},
			labelWidth:60,
			manager:Uec3.App.getDesktop().windows,
			items:[
			    {  
				    xtype:'combo',
					allowBlank : false,
					blankText : '零件编码不能为空',
					name : 'code',					
					fieldLabel:'零件编码',
					valueField:'code',
	                displayField : 'code',
   	  				mode : 'remote',
   	  				queryParam :'queryParam',
   	  				queryDelay:500,
   	  				minChars :2,
   	  				store : ds_code_select,
   	  				pageSize:10,
	   				selectOnFocus : true,
	   				triggerAction : 'all',
	   				loadingText : '加载中...',	   				
	   				emptyText : '请输入零件编码',
	   				listeners :{
					   'select':function(){
					   	  var value = this.getValue();
					      this.ownerCt.getForm().el.dom.reset();
					      this.setValue(value);
					   },
					   'change' : function() {
					   		this.ownerCt.getForm().el.dom.reset();
					   }
					}
			    }, {
			        xtype:'combo',
				    //width : 200,				   
					//name : 'source',
					fieldLabel:'零件来源',
					valueField:'varIndex',
	                displayField : 'varName',
   	  				mode : 'remote',
	   				hiddenName:'source',
	   				value:'',
	   				store : main_win.sourceType_store,
	   				selectOnFocus : true,
	   				forceSelection  :true,
	   				editable:true,
	   				triggerAction : 'all',
	   				loadingText : '加载中...',	   				
	   				emptyText : '请选择零件来源',	   				
	   				listeners :{
					   'select':autoFillDetailForm
					}
			    }, {
			        xtype:'textfield',
					allowBlank : false,
					blankText : '零件名称不能为空',
					name : 'name',
					fieldLabel:'零件名称',
					readOnly:true,
					style:{background:'#dddddd'}
			    }, {
			    	xtype:'textfield',				    
					allowBlank : false,
					blankText : '零件单位不能为空',
					name : 'measurementUnit',
					fieldLabel:'单位',
	   				readOnly:true,
	   				style:{background:'#dddddd'}
			    }, {
			    	xtype:'hidden',
			    	name : 'partId'
			    }, {
			    	xtype:'hidden',
			    	name : 'level1Id'
			    }, {
			    	xtype:'hidden',
			    	name : 'level1Name'
			    }, {
			    	xtype:'hidden',
			    	name : 'level2Name'
			    }, {
			        xtype:'numberfield',
					name : 'orderQuantity',
					fieldLabel:'采购数量',					
	                allowNegative: false	               	                
			    }, {
			        xtype:'numberfield',
					allowBlank : false,
					name : 'price',
					fieldLabel:'采购价',
					allowBlank: false,
	                allowNegative: false	               
			    }, {
			        xtype:'textfield',
					//width : 200,
					allowBlank : false,
					blankText : '品牌不能为空',
					name : 'brand',
					fieldLabel:'品牌'
			    }, {
			        xtype:'textfield',
					//width : 200,
					allowBlank : false,
					blankText : '产地不能为空',
					name : 'origin',
					fieldLabel:'产地'
			    }
			],
			buttons:[
			     {
			     	text:'提交',
			        handler:submitForm.createDelegate(main_win)
			     },{
			     	text:'重置',
			        handler:resetForm.createDelegate(main_win)
			     },{
			     	text:'关闭',
			     	handler:function(){
			     	    detail_wind.hide();
			     	}
			     }
			],
			buttonAlign:'center'
	    });
	   
	   function submitForm(){
	  	   //取得表单数据	  	 	  
	       if(!pur_detailForm.getForm().isValid()){
	          return;
	       }
	       
	       if((pur_detailForm.getForm().findField("orderQuantity").getValue()+'').trim() == '') {
	       		Ext.MessageBox.alert("提示", "采购数量不能为空.");
	       		return;
	       }
	       
	       pur_detailForm.enable();	    
	       var  newRecord = pur_detailForm.getForm().getValues();
	       var index = this.purchase_item_store.findExact('code',newRecord.code);
	       if(index != -1){
	       	   //修改明细
			   var item = this.purchase_item_store.getAt(index);
			   for(var fieldName in newRecord){
				   item.set(fieldName,newRecord[fieldName]);
			   }
			   this.purchase_item_store.commitChanges();			    
	       }else{
	          //增加明细
	       	  this.purchase_item_store.insert(this.purchase_item_store.getCount(), new this.purchase_item_record(newRecord));	       	  
              this.purchase_item_grid.getSelectionModel().selectFirstRow();
	       }
	       
	  	       pur_detailForm.form.el.dom.reset(); 
	  	       pur_detailForm.enable();
	  	       
	  }
	   
	   function resetForm(){
	  	       pur_detailForm.form.reset(); 
	  	       pur_detailForm.enable();
	  	    var columns = this.purchase_item_grid.getColumnModel().columns;
	        Ext.each(columns, function(value,index,arr) {
	             var cellName = value.dataIndex;
	             var field = pur_detailForm.form.findField(cellName);
				 if(!field.isDisable){
				    field.el.dom.value = '';
				 }		             
	        });
	  }
		
		//创建弹出窗口
		var detail_wind = new Ext.Window({
			layout:'fit',
		    width:380,
		    closeAction:'hide',
		    height:500,
			resizable :true,
			shadow : true,
			modal :true,
		    closable:true,
		    bodyStyle:'padding:5 5 5 5',
		    animCollapse:true,
			items:[pur_detailForm]
		});
		
		
		function addDetailData() {
			detail_wind.setTitle("新增订单明细");
			detail_wind.get(0).getForm().reset();
	        detail_wind.show();
		}
		
		function getToolBar() {
			//创建工具栏组件
			return new Ext.Toolbar({
			  items:[
				{text : '[新增明细]', iconCls : 'icon-add', handler : addDetailData}
			   ]
			});
	    }
		
		return main_win.join_panel;
	},
	/**
	 * 新建下一张
	 * 
	 */
	purNewCreate : function() {
		var desktop = Uec3.App.getDesktop();   
		desktop.launchWindowByParam("addOutPurchaseCreator",{});
	}
});